/*
	Dracula's Riddle Level 41 Solver
    Copyright (C) 2009  nightmare

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package lvl41;

import java.util.Scanner;

/**
 *	Cogs object - a "collection" of four cog objects
 * @author nightmare
 * @version 1.00
 */
public class Cogs {
	//Letters on each spoke of the cog read from the arrow clockwise
	private String[] letters1 = { "a", "g", "n", "v", "b", "w", "d", "i", "f", "s", "p", "c", "e", "h", "y", "k", "d", "t", "h", "z" };
	private String[] letters2 = { "r", "j", "k", "o", "g", "u", "e", "v", "b", "p", "d", "l", "m", "t", "a", "i", "u", "q", "o", "a" };
	private String[] letters3 = { "i", "q", "y", "h", "e", "n", "a", "a", "r", "w", "u", "g", "b", "t", "p", "c", "d", "l", "x", "k" };
	private String[] letters4 = { "t", "i", "n", "s", "l", "y", "c", "f", "h", "a", "r", "j", "m", "o", "a", "v", "z", "z", "b", "g" };

	//Four cogs constructed
	private Cog one = new Cog(1, "a", letters1);
	private Cog two = new Cog(2, "r", letters2);
	private Cog three = new Cog(3, "i", letters3);
	private Cog four = new Cog(4, "t", letters4);

	//Two dimensional array of type String that holds all the letters of each 
	//cog after being spun
	private String[][] matrix = new String[6][4];

	//Class that handles th einput from the command line for the questions asked
	Scanner s = new Scanner(System.in);

	/**
	 * Solves the riddle
	 * Asks what cog is to be turned and how many times it should be turned
	 * Then prints the matrix of letters that appear on the cogs after being 
	 * turned
	 */
	public void solve() {
		int spinNum = 0;
		while (spinNum < 6) {
			System.out.print("What cog is being spun? ");
			int cog = s.nextInt();
			System.out.print("How many times should you spin cog " + cog + "? ");
			int times = s.nextInt();
			spinAll(cog, times, spinNum);
			spinNum++;
		}
		printMatrix();
	}

	/**
	 * Prints the matrix after the level has been solved
	 */
	public void printMatrix() {
		System.out.println("\nTable of all letters from spun cogs");
		for (int row = 0; row < 6; row++) {
			for (int col = 0; col < 4; col++) {
				System.out.print(matrix[row][col] + "\t");
			}
			System.out.println();
		}
	}

	/**
	 * Spins all cogs in the correct direction as dictated by the cog being spun and populates
	 * the array with the letters that it ends up on
	 * @param cog the id of the cog that is currently being spun
	 * @param times the number of times that the cogs should be turned
	 * @param row the current iteration of cogs being spun (correlates to row of matrix being populated)
	 */
	public void spinAll(int cog, int times, int row) {
		boolean C = true;	//Clockwise
		boolean A = false;	//Anti-clockwise
		if (cog % 2 == 1) {		//Cogs 1 and 3
					matrix[row][0] = one.spin(times, C);
					matrix[row][1] = two.spin(times, A);
					matrix[row][2] = three.spin(times, C);
					matrix[row][3] = four.spin(times, A);
		} else {				//Cogs 2 and 4
					matrix[row][0] = one.spin(times, A);
					matrix[row][1] = two.spin(times, C);
					matrix[row][2] = three.spin(times, A);
					matrix[row][3] = four.spin(times, C);
		}
	}
}
